package com.example.androidclasseight.myadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidclasseight.bean.PYQBean;
import com.example.androidclasseight.R;

import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.Holder> {

    private Context context;
    private LinkedList<PYQBean> pyqBeans;

    public ChatAdapter(Context context, LinkedList<PYQBean> pyqBeans) {
        this.context = context;
        this.pyqBeans = pyqBeans;
    }

    @NonNull
    @org.jetbrains.annotations.NotNull
    @Override
    public ChatAdapter.Holder onCreateViewHolder(@NonNull @org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.rl_item_chat, null);
        return new Holder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull ChatAdapter.Holder holder, int position) {
        PYQBean pyqBean = pyqBeans.get(position);
        holder.titleIv.setImageDrawable(pyqBean.getAvatar());
        holder.subIv.setImageDrawable(pyqBean.getImage());
        holder.titleTv.setText(pyqBean.getUser());
        holder.subTv.setText(pyqBean.getText());
        holder.timeTv.setText(pyqBean.getData());
    }

    @Override
    public int getItemCount() {
        return pyqBeans.size();
    }

    public class Holder extends RecyclerView.ViewHolder {

        private ImageView titleIv, subIv;
        private TextView titleTv, subTv, timeTv;

        public Holder(@NonNull @NotNull View itemView) {
            super(itemView);
            titleIv = itemView.findViewById(R.id.item_tv);
            subIv = itemView.findViewById(R.id.item_iv_img);
            titleTv = itemView.findViewById(R.id.item_tv_title);
            subTv = itemView.findViewById(R.id.item_tv_subtext);
            timeTv = itemView.findViewById(R.id.item_time);
        }
    }
}
